package com.talent.jiwen_teacher.my;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.ui.widgets.SubsidyDialog;
import com.talent.jiwen_teacher.util.SPUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observable;

/**
 * Created by qyqx on 2018/8/14.
 * 订单界面
 */

public class OrderActivity extends BaseActivity {


    @BindView(R.id.indicator)
    MagicIndicator indicator;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_order;
    }

    @Override
    public String titleName() {
        return "我的订单";
    }

    @Override
    protected void initData() {

        titleList.clear();
        titleList.add(getString(R.string.have_finish));
        titleList.add(getString(R.string.no_finish));

        fragmentList.clear();
        fragmentList.add(new HaveFinishOrderFragment());
        fragmentList.add(new NoFinishOrderFragment());

        CommonNavigator commonNavigator = new CommonNavigator(mContext);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return titleList == null ? 0 : titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                commonPagerTitleView.setContentView(R.layout.item_indicator_order);

                final View indicator = (View) commonPagerTitleView.findViewById(R.id.indicator);
                final TextView text = (TextView)commonPagerTitleView.findViewById(R.id.text);

                String title = titleList.get(index);
                text.setText(title);

                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int i, int i1) {
                        indicator.setVisibility(View.VISIBLE);
                        text.setTextColor(ContextCompat.getColor(OrderActivity.this , R.color.common_app_color));
                    }

                    @Override
                    public void onDeselected(int i, int i1) {
                        indicator.setVisibility(View.GONE);
                        text.setTextColor(ContextCompat.getColor(OrderActivity.this , R.color.text_gray_9));
                    }

                    @Override
                    public void onLeave(int i, int i1, float v, boolean b) {

                    }

                    @Override
                    public void onEnter(int i, int i1, float v, boolean b) {

                    }
                });
                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewPager.setCurrentItem(index);
                    }
                });

                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        indicator.setNavigator(commonNavigator);

        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()){
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                return titleList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titleList.get(position);
            }
        });

        ViewPagerHelper.bind(indicator, viewPager);
    }

    SubsidyDialog subsidyDialog;

    public void showApplication() {

        mHeadRightText.setVisibility(View.VISIBLE);
        mHeadRightText.setText("申请补贴");
        mHeadRightText.setTextColor(getResources().getColor(R.color.common_app_color));
        mHeadRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    subsidyDialog=new SubsidyDialog(OrderActivity.this);
                    subsidyDialog.show();
                    subsidyDialog.setOnSubsidyClickListener(new SubsidyDialog.OnSubsidyClickListener() {
                        @Override
                        public void onSubsidyClick() {
                            teacherApplyDoubleReward();
                        }
                    });
            }
        });

    }

    public void hideApplication() {

        mHeadRightText.setVisibility(View.GONE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(subsidyDialog!=null && subsidyDialog.isShowing()){
            subsidyDialog.dismiss();
        }
    }


    /**申请补贴
     *
     */
    private void teacherApplyDoubleReward(){
        Observable observable = ApiFactory.getEducationApiSingleton().teacherApplyDoubleReward(SPUtil.getToken());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(OrderActivity.this) {
            @Override
            protected void _onNext(Void v) {
                showToast("申请成功,请等待审核");
                hideApplication();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        } , ActivityLifeCycleEvent.DESTROY , lifecycleSubject , false);

    }
}
